import React, { Component } from 'react';
import './App.css';

class App extends Component {
  constructor(props) {
    super(props)
    this.state = { date: new Date()}
  }
  componentDidMount () { // 组件挂载完成调用
    this.timeID = setInterval(() => {
      this.tick()
    }, 1000);
  }
  componentWillUnmount () { // 组件销毁前调用
    clearInterval(this.timeID)
  }
  tick () {
    this.setState({
      date: new Date()
    })
  }
  render () {
    return (
      <div>
        {this.state.date.toLocaleTimeString()}
      </div>
    );
  }
}

export default App;
